package com.ServiceImpl;

import com.Service.MenuService;
import com.dao.CarDao;
import com.dao.RecordDao;
import com.dao.UserDao;
import com.pojo.Car;
import com.pojo.Record;
import com.pojo.User;

import java.math.BigDecimal;
import java.util.List;

public class MenuServiceImpl implements MenuService {

    @Override
    public List<Car> getAllCar(User user) {
        String sql = "select id,name,remark,brand,type,price,rent,status,cardNumber,carPrice,color from car";
        if (!user.isAdmin()) {
            sql += " where status=1";
        }
        List<Car> cars = CarDao.getAllCarsBySql(sql);

        return cars;
    }

    @Override
    public Car getCarByid(User user, String id) {
        Car car = CarDao.getCarById(id);
        if (user.isAdmin()) {
            return car;
        } else {
            if (car.isStatus()) {
                return car;
            }
        }
        return null;
    }

    @Override
    public boolean addCar(Car car) {
        Object carOB[] = {car.getId(), car.getName(), car.getRemark(), car.getBrand(), car.getType(), car.getPrice(), car.isRent(), car.isStatus(), car.getCardNumber(), car.getCarPrice(), car.getColor()};
        boolean a = CarDao.addCar(carOB);
        return a;
    }

    @Override
    public boolean updateCarById(String id, String key, BigDecimal value) {
        boolean result = false;
        if (key.equals("price")) {
            result = CarDao.updateCarPriceById(id, value);
        } else if (key.equals("status")) {
            System.out.println(value);
            int valueInt = value.intValue();
            if (valueInt == 1) {
                result = CarDao.updateCarStatusById(id, true);
            } else if (valueInt == 2) {
                result = CarDao.updateCarStatusById(id, false);
            } else {
                result = false;
            }
        }
        return result;
    }

    @Override
    public List<User> getAllUser() {
        List<User> allUser = UserDao.getUserList();
        return allUser;
    }

    @Override
    public List<Car> getSortCar(String choice, User user) {
        String sql = "select id,name,remark,brand,type,price,rent,status,cardNumber,carPrice,color from car";
        if (!user.isAdmin()) {
            sql += " where rent=1";
        }
        if (choice.equals("1")) {
            sql += " order by price asc";
        } else if (choice.equals("2")) {
            sql += " order by price desc";
        }
        List<Car> allCars = CarDao.getAllCarsBySql(sql);
        return allCars;

    }

    @Override
    public List<Car> getCarByType(String type, User user) {
        String sql = "SELECT id,name,remark,brand,type,price,rent,status,cardNumber,carPrice,color FROM car where type='" + type+"'";
        if (!user.isAdmin()) {
            sql += " AND status=1";
        }
        List<Car> cars = CarDao.getAllCarsBySql(sql);
        return cars;
    }

    @Override
    public List<Car> getCarByBrand(String brand, User user) {
        String sql = "SELECT id,name,remark,brand,type,price,rent,status,cardNumber,carPrice,color FROM car where brand='" + brand+"'";
        if (!user.isAdmin()) {
            sql += " AND status=1";
        }
        List<Car> cars = CarDao.getAllCarsBySql(sql);
        return cars;
    }

    @Override
    public Record rentCarById(String id, User user) {
                Car car = CarDao.getCarById(id);
                if (car==null){
                    System.out.println("没有找到此辆车");
                    return null;
                }
                if (!car.isRent()) {
                    System.out.println("此车不可租");
                    return null;
                } else {
                    String sql = "select * from record";
                    List<Record> allRecord = RecordDao.getRecordBySql(sql);
                    Record record = new Record("下面定义", user.getId(), car.getId());

                    if (allRecord.size() == 0) {
                        record.setId("101");
                    } else {
                        record.setId((Integer.parseInt(allRecord.get(allRecord.size() - 1).getId()) + 1) + "");
                    }
                    //生成订单
                    boolean result = RecordDao.addRecord(record);
                    //修改车辆的属性
                    CarDao.updateRentById(record.getCar_id(), false);
                    Record recordByid = RecordDao.getRecordByid(record.getId());
                    return result ? recordByid:null;
        }
    }

    @Override
    public Record backCar(String id,BigDecimal price) {
        RecordDao.setRecord(id,price);
        Record record = RecordDao.getRecordByid(id);
        CarDao.updateRentById(record.getCar_id(), true);

        return record;
    }

    @Override
    public List<Record> getRecordsByUser(User user) {
        String sql = "select id,user_id,car_id,rentTime,backTime,price from record ";
        if (!user.isAdmin()) {
            sql += " where user_id =" + user.getId()+"";
        }
        List<Record> allRecord = RecordDao.getRecordBySql(sql);

        return allRecord;
    }

    @Override
    public List<Record> getAllRecords() {
        String sql = "select id,user_id,car_id,rentTime,backTime,price from record";
        List<Record> allRecord = RecordDao.getRecordBySql(sql);

        return allRecord;
    }

    @Override
    public List<Record> getRecordsByCar(Car car) {
        String sql = "select id,user_id,car_id,rentTime,backTime,price from record where car_id=" + car.getId();
        List<Record> allRecord = RecordDao.getRecordBySql(sql);
        return allRecord;
    }

    @Override
    public Record getRecordByid(String id) {
        return RecordDao.getRecordByid(id);
    }

    @Override
    public User getUserById(String id) {
        return UserDao.getUserById(id);
    }

}
